package com.jtny.nytb.equipment.vo.excel;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.jtny.component.entity.WebResponse;
import com.jtny.nytb.equipment.enums.ExcelCodeRuleEnum;
import lombok.Getter;

import java.util.List;
import java.util.Map;

/**
 * @author 智慧能源中心 - 伍天宇
 * @package com.jtny.nytb.energymonitor.equipment.vo.excel
 * @fileName ExcelResultVO.java
 * @createTime 2023年11月23日 10:08
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@Getter
public class ExcelResultVO {

    /**
     * 成功消息
     */
    private Integer successCount;

    /**
     * 错误信息
     */
    private String errorMessage;

    public ExcelResultVO() {
        this.successCount = 0;
        this.errorMessage = "";
    }

    public ExcelResultVO(Integer successCount) {
        this.successCount = successCount;
        this.errorMessage = "";
    }

    public ExcelResultVO(String errorMessage) {
        this.successCount = 0;
        this.errorMessage = errorMessage;
    }

    public ExcelResultVO(Integer successCount, String errorMessage) {
        this.successCount = successCount;
        this.errorMessage = errorMessage;
    }

    public ExcelResultVO(List<ExcelResultVO> excelResultVOList) {
        ExcelResultVO result = new ExcelResultVO();
        // 求和
        int count = excelResultVOList.stream().mapToInt(ExcelResultVO::getSuccessCount).sum();
        String errorMessage = excelResultVOList.stream()
                .map(ExcelResultVO::getErrorMessage)
                .reduce("", String::concat);
    }

    public ExcelResultVO(ExcelResultVO oldResult, ExcelResultVO newResult) {
        if (ObjectUtil.isNotEmpty(oldResult) && ObjectUtil.isNotEmpty(newResult)) {
            this.successCount = oldResult.getSuccessCount() + newResult.getSuccessCount();
            this.errorMessage = oldResult.getErrorMessage() + StrUtil.LF + newResult.getErrorMessage();
        } else if (ObjectUtil.isNotEmpty(oldResult)) {
            this.successCount = oldResult.getSuccessCount();
            this.errorMessage = oldResult.getErrorMessage();
        } else {
            this.successCount = newResult.getSuccessCount();
            this.errorMessage = newResult.getErrorMessage();
        }
    }

    public void setSuccessCount() {
        this.successCount = this.successCount != null ? this.successCount + 1 : 1;
    }

    public void setSuccessCount(Integer successCount) {
        this.successCount = successCount;
    }


    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public void setCodeRuleMessage(String logName, ExcelCodeRuleEnum ruleEnum) {
        this.errorMessage = this.errorMessage + logName + "导入失败，原因是：" + ruleEnum.getName() + "不存在" + StrUtil.LF;
    }


    public void setDuplicateMessage(String duplicate) {
        this.errorMessage = this.errorMessage + duplicate + "已存在，不可重新导入";
    }

    public String getAllMessage() {
        if (StrUtil.isBlank(errorMessage)) {
            return successCount + "条数据导入成功";
        }
        if (successCount == 0) {
            return "数据导入失败" + this.errorMessage;
        }
        return this.successCount + "条数据导入成功，其中" + this.errorMessage;
    }

    public void setNoExistEquipmentModelMessage(Map<String, String> noExistEquipmentModelMap) {
        if (!noExistEquipmentModelMap.isEmpty()) {
            StringBuilder errorMessage = new StringBuilder();
            for (String equipmentName : noExistEquipmentModelMap.keySet()) {
                errorMessage.append("设备名称：").append(equipmentName)
                        .append("使用的设备模型：").append(noExistEquipmentModelMap.get(equipmentName))
                        .append("不存在").append(StrUtil.LF);
            }
            this.errorMessage = this.errorMessage + errorMessage + "请先保证使用的设备模型正确，再导入";
        }
    }

    public void setNoExistEquipmentAreaMessage(Map<String, String> noExistEquipmentAreaMap) {
        if (!noExistEquipmentAreaMap.isEmpty()) {
            StringBuilder errorMessage = new StringBuilder();
            for (String equipmentName : noExistEquipmentAreaMap.keySet()) {
                errorMessage.append("设备名称：").append(equipmentName)
                        .append("使用的区域：").append(noExistEquipmentAreaMap.get(equipmentName))
                        .append("不存在").append(StrUtil.LF);
            }
            this.errorMessage = this.errorMessage + errorMessage + "请先保证使用的区域正确，再导入";
        }
    }

    public WebResponse getWebResponse() {
        if (successCount > 0) {
            WebResponse<Object> webResponse = new WebResponse<>();
            webResponse.setCode("205");
            webResponse.setMessage(getAllMessage());
            return webResponse;
        }
        return WebResponse.error(getAllMessage());
    }

}
